import { getFileList, } from '@/service/modules/file'
import { IFileList } from '@/types';
import { Effect, ImmerReducer, Reducer, Subscription } from 'umi';

export interface FileModelState {
    fileList: IFileList[],

}


export interface FileModelType {
    namespace: 'file';
    state: FileModelState;
    effects: {
        getFileList: Effect;

    };
    reducers: {
        save: Reducer<FileModelState>;
    };
}

const FileModel: FileModelType = {
    // 数据模型，用来隔离数据防止相互影响
    namespace: 'file',

    // 数据模型状态，相当于vuex的state
    state: {
        fileList: [],

    },

    // 异步操作，相当于vuex的action
    effects: {
        // 先简单记忆，*相当于async，yield相当于await
        *getFileList({ payload }, { call, put }) {
            let result = yield call(getFileList, payload);
            console.log(result, "----------------------");

            if (result.data) {
                yield put({
                    type: 'save',
                    payload: {
                        fileList: result.data[0],
                        fileLength: result.data[1]
                    }
                })
            }
        },

    },

    // 同步操作，相当于vuex的mutation
    reducers: {
        save(state, action) {
            return {
                ...state,
                ...action.payload,
            };
        },
    },
};

export default FileModel;
